import request from '../utils/request.js'
import { useLoginStore } from '../stores/login.store.js'
const loginStore = useLoginStore()

// 获取商品列表数据
export const getGoodList = () => {
	return request.get('/api/recommend?page=1&limit=10')
}

//  获取历史记录商品
export const getHistory = () => {
	return request.get('/api/history?page=1&pagesize=10')
}

//  获取订单
export const getUserOrder = () => {
	return request.post('/api/userorder')
}

export const reqUploadAvatar = (tempFilePath) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `https://letaoapi.jxsaichuang.com/api/upload`, // 确保baseURL已配置
			filePath: tempFilePath,
			name: 'avatar', // 与后端接收的字段名一致
			header: {
				'token': loginStore.Token, // 添加token到请求头
				'Content-Type': 'multipart/form-data'
			},
			formData: {
				'type': 'avatar'
			},
			success: (uploadRes) => {
				try {
					const res = JSON.parse(uploadRes.data)
					if (res.status === 0) {
						resolve({
							code: 200,
							data: {
								avatarUrl: res.src
							}
						})
					} else {
						reject(new Error(res.message || '上传失败'))
					}
				} catch (e) {
					reject(new Error('解析响应数据失败'))
				}
			},
			fail: (err) => {
				reject(new Error(err.errMsg || '上传失败'))
			}
		})
	})
}